/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 * MIFSS  - content storage system
 * 
 *
 * @uthors: uros.kristan@gmail.com (Uroš Kristan ) Urosk.NET
 *         jernej.svigelj@gmail.com (Jernej Švigelj) 
 */
package net.monare.mifss.lib.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.monare.mifss.configurations.pojo.ContentMetaDataDef;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;

public class ContentMetaDataMapper implements RowMapper<ContentMetaDataDef> {

	public static final String ID_FIELD = "ID_CONTENT";

	private List<String> fieldList = new ArrayList<String>();
	{
		fieldList.add("ID_CONTENT");
		fieldList.add("UUID");
		fieldList.add("FILE_HASH");
		fieldList.add("FILE_HASH_METHOD");
		fieldList.add("MIME_TYPE");
		fieldList.add("INSERTED");
		fieldList.add("FILE_NAME");
		fieldList.add("TITLE");
		fieldList.add("ORIGINAL_FILE_PATH");
		fieldList.add("STORAGE_NAME");
		fieldList.add("ID_CONTENT_POOL");
		fieldList.add("ID_USER");
		fieldList.add("DATA_SIZE");
		fieldList.add("META_DATA");
		fieldList.add("EXTENSION");
		fieldList.add("TRANSFORMATION");

	}

	public List<String> getFieldList() {
		return fieldList;
	}

	public MapSqlParameterSource getNamedParameters(ContentMetaDataDef cm) {

		MapSqlParameterSource namedParameters = new MapSqlParameterSource();

		namedParameters.addValue("ID_CONTENT", cm.getIdContent());
		namedParameters.addValue("UUID", cm.getUuid());
		namedParameters.addValue("FILE_HASH", cm.getFileHash());
		namedParameters.addValue("FILE_HASH_METHOD", cm.getFileHashMethod());

		namedParameters.addValue("MIME_TYPE", cm.getMimeType());
		namedParameters.addValue("INSERTED", cm.getInserted());
		namedParameters.addValue("FILE_NAME", cm.getFileName());
		namedParameters.addValue("TITLE", cm.getTitle());
		namedParameters.addValue("ORIGINAL_FILE_PATH", cm.getOriginalFilePath());
		namedParameters.addValue("STORAGE_NAME", cm.getStorageName());
		namedParameters.addValue("ID_CONTENT_POOL", cm.getIdContentPool());
		namedParameters.addValue("ID_USER", cm.getIdUser());
		namedParameters.addValue("DATA_SIZE", cm.getDataSize());
		namedParameters.addValue("META_DATA", cm.getMetaData());
		namedParameters.addValue("EXTENSION", cm.getExtension());
		namedParameters.addValue("TRANSFORMATION", cm.getTransformation());

		return namedParameters;
	}

	public ContentMetaDataDef mapRow(ResultSet rs, int rowNum) throws SQLException {

		ContentMetaDataDef cm = new ContentMetaDataDef();

		cm.setIdContent(rs.getLong("ID_CONTENT"));
		cm.setUuid(rs.getString("UUID"));
		cm.setFileHash(rs.getString("FILE_HASH"));
		cm.setFileHashMethod(rs.getString("FILE_HASH_METHOD"));
		cm.setMimeType(rs.getString("MIME_TYPE"));
		cm.setInserted(rs.getDate("INSERTED"));
		cm.setFileName(rs.getString("FILE_NAME"));
		cm.setTitle(rs.getString("TITLE"));
		cm.setOriginalFilePath(rs.getString("ORIGINAL_FILE_PATH"));
		cm.setStorageName(rs.getString("STORAGE_NAME"));
		cm.setIdContentPool(rs.getInt("ID_CONTENT_POOL"));
		cm.setIdUser(rs.getLong("ID_USER"));
		cm.setDataSize(rs.getLong("DATA_SIZE"));
		cm.setMetaData(rs.getString("META_DATA"));
		cm.setExtension(rs.getString("EXTENSION"));
		cm.setTransformation(rs.getInt("TRANSFORMATION"));

		return cm;
	}

}
